﻿using MediatR;
using MyRabbit.Application.Command.SlaughteCommand;
using MyRabbit.Application.ErrorCode;
using MyRabbit.Domain;
using MyRabbit.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyRabbit.Application.CommandHandler.SlaughteCommandHandler
{
    public class SlaughteQueryCommandHandler : IRequestHandler<SlaughteQueryCommand, APIResult<Slaughte>>
    {
        /// <summary>
        /// 屠宰仓储
        /// </summary>
        private readonly IRepository<Slaughte> slaughteRep;
        /// <summary>
        /// 产品仓储
        /// </summary>
        private readonly IRepository<Product> proRep;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="slaughteRep"></param>
        /// <param name="proRep"></param>
        public SlaughteQueryCommandHandler(IRepository<Slaughte> slaughteRep, IRepository<Product> proRep)
        {
            this.proRep = proRep;
            this.slaughteRep = slaughteRep;
        }




        public async Task<APIResult<Slaughte>> Handle(SlaughteQueryCommand request, CancellationToken cancellationToken)
        {
            APIResult<Slaughte> result = new APIResult<Slaughte> ();
            result.Code=Codes.查询成功;
            result.Message=Codes.查询成功.ToString();

            try
            {
                var data = await proRep.GetByConditionAsync(w => w.ProductId == request.ProductId);
                result.Data = await slaughteRep.GetByConditionAsync(w => w.SlaughteId == data.SlaughteId);
            }
            catch (Exception exp)
            {

                result.Code = Codes.查询失败;
                result.Message = Codes.查询失败.ToString()+exp.Message;
            }



            return await Task.FromResult (result);
        }
    }
}
